package com.ftg.midamall.service;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ftg.midamall.comm.tool.TokenUtil;
import com.ftg.midamall.comm.vo.CodeStatusEnum;
import com.ftg.midamall.comm.vo.ConstUtil;
import com.ftg.midamall.comm.vo.RespMessUtil;
import com.ftg.midamall.dto.UserInfo;
import com.ftg.midamall.feign.UserCenterFeignClient;
import com.ftg.midamall.vo.UmsAdminVo;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Objects;

/**
 * @author lijiaxin
 * @version 1.0
 * @date 2023/10/19 10:49
 */
@Service
public class UserCenterService {

    @Resource
    UserCenterFeignClient userCenterFeignClient;

    public RespMessUtil<String> accessUmsAdminLogin(String username, String password){
        UserInfo userinfo = new UserInfo();
        userinfo.setUsername(username);
        userinfo.setPassword(password);
        RespMessUtil<UmsAdminVo> login = userCenterFeignClient.login(userinfo);
        if(Objects.nonNull(login) && login.getCode() == 200){
            String jsonString = JSON.toJSONString(login);
            JSONObject jsonObject = JSONObject.parseObject(jsonString);

            String token = TokenUtil.createToken(jsonObject);
            return RespMessUtil.build(CodeStatusEnum.SUCCESS,token);
        }else{
            return RespMessUtil.build(CodeStatusEnum.FAILURE);
        }
    }

}
